package com.kitty.game.equip.service;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.base.service.BagService;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.confirm.model.DWDRConfirm;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.Leiji;
import com.kitty.game.equip.model.LeijiData;
import com.kitty.game.fight.service.OneThreadService;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespConfirm;
import com.kitty.game.utils.CommonConst;
import com.kitty.game.welfare.model.CommonFetchedData;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

@Service
@Slf4j
public class DWDRService {
    @Autowired
    FabaoService fabaoService;
    @Autowired
    RoleService roleService;
    @Autowired
    Dao dao;
    @Autowired
    ChatService chatService;
    CommonFetchedData commonFetchedData;
    LinkedHashMap<String, NutMap> data = new LinkedHashMap<>();


    public void init() {
        String[] names = {"混元金斗", "番天印", "阴阳镜", "定海珠", "金蛟剪", "九龙神火罩", "卸甲金葫"};
        String[] xiangxins = {"金", "木", "水", "火", "土"};
        int index = 0;
        for (String name : names) {
            for (String xiangxin : xiangxins) {
                index++;
                String comtent = "24级" + name + "(" + xiangxin + ")";
                NutMap nutMap = new NutMap();
                nutMap.setv("name", comtent);
                nutMap.setv("jifen", 1388);
                data.put("dwdr0000" + index, nutMap);
            }
        }
        commonFetchedData = dao.fetch(CommonFetchedData.class, CommonConst.leiji_reward);
        if (commonFetchedData == null) {
            commonFetchedData = new CommonFetchedData();
            commonFetchedData.setData(Json.toJson(new LeijiData()));
            commonFetchedData.setType(CommonConst.leiji_reward);
            dao.insertOrUpdate(commonFetchedData);
        }
    }

    public void add(Role role) {
        SpringUtils.getBean(OneThreadService.class).addFightTask(0, new Runnable() {
            @Override
            public void run() {
                String content = commonFetchedData.getData();
                LeijiData leijiData = Json.fromJson(LeijiData.class, content);
                Leiji leiji = leijiData.getData().get(role.getRoleId());
                if (leiji != null) {
                    leiji.setCount(leiji.getCount() + 1);
                    leiji.setName(role.getName());
                } else {
                    leiji = new Leiji();
                    leiji.setCount(leiji.getCount() + 1);
                    leiji.setName(role.getName());
                    leiji.setRoleId(role.getRoleId());
                    leijiData.getData().put(role.getRoleId(), leiji);
                }
                commonFetchedData.setData(Json.toJson(leijiData));
                dao.insertOrUpdate(commonFetchedData);
            }
        });
    }


    public String point(Role role) {
        String content = commonFetchedData.getData();
        if (content == null) {
            return null;
        }
        LeijiData leijiData = Json.fromJson(LeijiData.class, content);
        Leiji leiji = leijiData.getData().get(role.getRoleId());
        if (leiji == null) {
            return null;
        }
        if (leiji.getCount() <= 0) {
            return null;
        }
        String head = "累计充值可以在我这兑换好礼哦！！！";
        for (Map.Entry<String, NutMap> entry : data.entrySet()) {
            NutMap nutMap = entry.getValue();
            head = head + "[" + nutMap.getString("name") + "/" + entry.getKey() + "]";
        }
        String tail = "[离开]";
        return tail;
    }


    public boolean buy(Role role, String msg) {
        NutMap nutMap = data.get(msg);
        if (nutMap == null) {
            return true;
        }
        role.setConfirm(new DWDRConfirm(msg));
        msg = "确认领取累计充值奖励#R" + nutMap.getString("name") + "#n？";
        RespConfirm respConfirm = new RespConfirm();
        respConfirm.setTips(msg);
        MessagePusher.pushMessage(role, respConfirm);
        return true;
    }

    public boolean besureBuy(Role role) {
        SpringUtils.getBean(OneThreadService.class).addFightTask(0, new Runnable() {
            @Override
            public void run() {
                DWDRConfirm dwdrConfirm = (DWDRConfirm) role.getConfirm();
                String msgId = dwdrConfirm.getMsg();
                NutMap nutMap = data.get(msgId);
                if (nutMap == null) {
                    return;
                }
                String content = commonFetchedData.getData();
                if (content == null) {
                    return;
                }
                short pos = SpringUtils.getBean(BagService.class).getPos(role, false);
                if (pos <= 0) {
                    MessagePusher.pushMessage(role,new RespNotifyMiscEx("背包已满"));
                    return;
                }
                LeijiData leijiData = Json.fromJson(LeijiData.class, content);
                Leiji leiji = leijiData.getData().get(role.getRoleId());
                if (leiji == null) {
                    return;
                }
                if (leiji.getCount() <= 0) {
                    return;
                }
                leiji.setCount(leiji.getCount() - 1);
                commonFetchedData.setData(Json.toJson(leijiData));
                dao.insertOrUpdate(commonFetchedData);
                String notice = null;
                String name = null;
                int xiangxin = 0;
                if (nutMap.getString("name").contains("(金)")) {
                    xiangxin = 1;
                } else if (nutMap.getString("name").contains("(木)")) {
                    xiangxin = 2;
                } else if (nutMap.getString("name").contains("(水)")) {
                    xiangxin = 3;
                } else if (nutMap.getString("name").contains("(火)")) {
                    xiangxin = 4;
                } else if (nutMap.getString("name").contains("(土)")) {
                    xiangxin = 5;
                }
                String temp = nutMap.getString("name");
                notice = "惊闻玩家#R" + role.getName() + "#n在#R多闻道人#n处获得累计充值奖励#R" + temp + "#n！真是可喜可贺！#51 #82";
                String equipname = temp.substring(3, temp.indexOf("("));
                if (nutMap.getString("name").contains("24")) {
                    fabaoService.getArtifact24(role, equipname, xiangxin);
                } else {
                    fabaoService.getArtifact18(role, equipname, xiangxin);
                }
                log.error("累计充值奖励==={}=={}", role.getName(), Json.toJson(nutMap, JsonFormat.compact()));
                chatService.sendAdnotice(notice);
            }
        });

        return true;
    }

}

